package cn.zn.modules.pz.his.service.impl;

import cn.zn.modules.pz.enums.MoneyTypeEnum;
import cn.zn.modules.pz.his.entity.PzMoney;
import cn.zn.modules.pz.his.entity.QPzMoney;
import cn.zn.modules.pz.his.entity.vo.PzMoneyVO;
import cn.zn.modules.pz.his.repository.PzMoneyRepository;
import cn.zn.modules.pz.his.service.PzMoneyService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
@Slf4j
public class PzMoneyServiceImpl implements PzMoneyService {

    @Autowired
    private PzMoneyRepository pzMoneyRepository;

    @Override
    public List<PzMoneyVO> findByMainId(Integer ap, String mainId) {
        List<PzMoneyVO> pzMoneyVOS = Lists.newArrayList();
        QPzMoney qPzMoney = QPzMoney.pzMoney;
        Iterator<PzMoney> iterator = pzMoneyRepository.findAll(qPzMoney.ap.eq(ap).and(qPzMoney.mainId.eq(mainId))).iterator();
        PzMoney pzMoney;
        while (iterator.hasNext()){
            pzMoney = iterator.next();
            pzMoneyVOS.add(PzMoneyVO.builder()
                    .moneyTypeName(MoneyTypeEnum.findByCode(pzMoney.getMoneyTypeCode()).getName())
                    .projectName(pzMoney.getProjectName())
                    .cateName(pzMoney.getCateName())
                    .money(pzMoney.getMoney())
                    .build());
        }
        return pzMoneyVOS;
    }
}
